package cn.sptpc.dao.impl;

import cn.sptpc.dao.ActivityDao;
import cn.sptpc.entity.java.Activity;
import cn.sptpc.entity.java.User;
import cn.sptpc.utils.DBUtil;
import cn.sptpc.utils.TimeUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ActivityDaoI implements ActivityDao {

    private DBUtil dbUtil = DBUtil.getDbUtil();

    /**
     * 查询接收的部门活动
     * @param pageUtil
     * @param user
     * @return
     */
    @Override
    public List<Activity> queryAllUser(int[] pageUtil, User user){
        Connection con = dbUtil.getConnection();
        List<Activity> activities = new ArrayList<>();
        PreparedStatement pstm;
        String sql = "";
        long upermissions = user.getPermissions();
        try {

            if (upermissions == 0){
                sql = "select * from oa_activity limit ?,?";
            }else {
                sql = "select * from oa_activity where dep_id = ? OR sub_dep_id = ? limit ?,?";
            }
            pstm =con.prepareStatement(sql);
            if (upermissions == 0){
                pstm.setLong(1,pageUtil[0]);
                pstm.setLong(2,pageUtil[1]);
            }else {
                int departmentId = (int)user.getDepartmentId();
                pstm.setLong(1,departmentId);
                pstm.setLong(2,departmentId);
                pstm.setLong(3,pageUtil[0]);
                pstm.setLong(4,pageUtil[1]);
            }
            ResultSet rs = pstm.executeQuery();
            while (rs.next()){
                Activity activity = new Activity();
                activity.setId(rs.getLong("id"));
                activity.setName(rs.getString("name"));
                activity.setHoldingDay(rs.getString("holding_day"));
                activity.setSubmittedDay(TimeUtil.stampToDate(rs.getString("submitted_day")));
                activity.setPassDay(TimeUtil.stampToDate(rs.getString("pass_day")));
                activity.setSubmitUserId(rs.getLong("submit_user_id"));
                activity.setSubUsername(rs.getString("sub_username"));
                activity.setSubDepId(rs.getLong("sub_dep_id"));
                activity.setSubDepName(rs.getString("sub_dep_name"));
                activity.setDocpath(rs.getString("docpath"));
                activity.setDepId(rs.getLong("dep_id"));
                activity.setDepName(rs.getString("dep_name"));
                activity.setDistribute(rs.getLong("distribute"));
                activities.add(activity);
            }
            return activities;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 添加一个活动
     *
     * @param activity
     * @return
     */
    @Override
    public Integer insertActivity(Activity activity) {
        Connection con = dbUtil.getConnection();
        String sql = "insert into activity (name, holdingDay, submittedDay, submituser,submit_department, docname, department, distribute) values (?,?,?,?,?,?,?,?)";
        Integer i;
        try {
            PreparedStatement pstm = con.prepareStatement(sql);
            pstm.setString(1,activity.getName());
            pstm.setString(2,activity.getHoldingDay());
            pstm.setString(3,activity.getSubmittedDay());
            pstm.setLong(4,activity.getSubmitUserId());
            pstm.setLong(5,activity.getSubDepId());
            pstm.setString(6,activity.getDocpath());
            pstm.setLong(7,activity.getDepId());
            pstm.setLong(8,activity.getDistribute());
            i = pstm.executeUpdate();
            return i;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据ID查询活动
     * @param id
     * @return
     */
    @Override
    public Activity selectActivity(String id) {
        Connection con = dbUtil.getConnection();
        String sql = "select * from oasystem.oa_activity where id = ?";
        try {
            PreparedStatement pstm = con.prepareStatement(sql);
            pstm.setString(1,id);
            ResultSet rs = pstm.executeQuery();
            Activity activity = new Activity();
            while (rs.next()){
                activity.setId(rs.getLong("id"));
                activity.setName(rs.getString("name"));
                activity.setHoldingDay(rs.getString("holding_day"));
                activity.setSubmittedDay(TimeUtil.stampToDate(rs.getString("submitted_day")));
                activity.setPassDay(TimeUtil.stampToDate(rs.getString("pass_day")));
                activity.setSubmitUserId(rs.getLong("submit_user_id"));
                activity.setSubUsername(rs.getString("sub_username"));
                activity.setSubDepId(rs.getLong("sub_dep_id"));
                activity.setSubDepName(rs.getString("sub_dep_name"));
                activity.setDocpath(rs.getString("docpath"));
                activity.setDepId(rs.getLong("dep_id"));
                activity.setDepName(rs.getString("dep_name"));
                activity.setDistribute(rs.getLong("distribute"));
            }
            return activity;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 模糊查询
     * @param key
     * @param iUser
     * @return
     */
    public List<Activity> queryActivity(String key,User iUser){
        Connection con = dbUtil.getConnection();
        List<Activity> activities = new ArrayList<>();
        int permissions = (int) iUser.getPermissions();
        String sql = "";
        if (permissions == 0){
            sql = "select  * from oa_activity where id = ? OR name = ?";
        }else{
            sql = "select * from oa_activity where (id = ? or name = ?) or (sub_dep_id = ? or dep_id = ?)";
        }
        try {
            PreparedStatement pstm = con.prepareStatement(sql);
            if (permissions == 0){
               pstm.setString(1,key);
               pstm.setString(2,key);
            }else{
                pstm.setString(1,key);
                pstm.setString(2,key);
                pstm.setLong(3,iUser.getDepartmentId());
                pstm.setLong(4,iUser.getDepartmentId());
            }
            ResultSet rs = pstm.executeQuery();
            while (rs.next()){
                Activity activity = new Activity();
                activity.setId(rs.getLong("id"));
                activity.setName(rs.getString("name"));
                activity.setHoldingDay(rs.getString("holding_day"));
                activity.setSubmittedDay(TimeUtil.stampToDate(rs.getString("submitted_day")));
                activity.setPassDay(TimeUtil.stampToDate(rs.getString("pass_day")));
                activity.setSubmitUserId(rs.getLong("submit_user_id"));
                activity.setSubUsername(rs.getString("sub_username"));
                activity.setSubDepId(rs.getLong("sub_dep_id"));
                activity.setSubDepName(rs.getString("sub_dep_name"));
                activity.setDocpath(rs.getString("docpath"));
                activity.setDepId(rs.getLong("dep_id"));
                activity.setDepName(rs.getString("dep_name"));
                activity.setDistribute(rs.getLong("distribute"));
                activities.add(activity);
            }
            return activities;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Integer deleteActivity(Long id) {
        Connection con = dbUtil.getConnection();
        String sql = "delete  from activity where id = ?";
        try {
            PreparedStatement pstm = con.prepareStatement(sql);
            pstm.setLong(1,id);
            int i = pstm.executeUpdate();
            return i;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Integer updateActivity(Activity activity) {
        Connection con = dbUtil.getConnection();
        String sql = "update activity set passDay = ?,distribute = ? where id = ?";
        try {
            PreparedStatement pstm = con.prepareStatement(sql);
            pstm.setString(1,activity.getPassDay());
            pstm.setLong(2,activity.getDistribute());
            pstm.setLong(3,activity.getId());
            int i = pstm.executeUpdate();
            return i;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

}
